﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.Collections.ObjectModel;

namespace SaveFuel
{
    public partial class Configuration : PhoneApplicationPage
    {

        private ObservableCollection<UnitModel> distanceUnits = new ObservableCollection<UnitModel>();
        private ObservableCollection<UnitModel> moneyUnits = new ObservableCollection<UnitModel>();
        private ObservableCollection<UnitModel> volumeUnits = new ObservableCollection<UnitModel>();

        public Configuration()
        {
            InitializeComponent();
        }

        private void GPSToggle_Checked(object sender, RoutedEventArgs e)
        {
            using (GestioneDBContext context = new GestioneDBContext(GestioneDBContext.ConnectionString))
            {
                context.Settings.First().GPS = true;
                context.SubmitChanges();
                App.isGPSEnabled = true;
            }
        }

        private void GPSToggle_Unchecked(object sender, RoutedEventArgs e)
        {
            using (GestioneDBContext context = new GestioneDBContext(GestioneDBContext.ConnectionString))
            {
                context.Settings.First().GPS = false;
                context.SubmitChanges();
                App.isGPSEnabled = false;
            }
        }

        private void GPSToggle_Loaded(object sender, RoutedEventArgs e)
        {
            GPSToggle.IsChecked = App.isGPSEnabled;
        }

        private void DistanceUnit_Loaded(object sender, RoutedEventArgs e)
        {
            #region Initialize Distance Unit

            UnitModel kmUnitModel = new UnitModel();
            kmUnitModel.UnitAbbr = "km";
            kmUnitModel.UnitDescription = DistanceUnitKm.Text;

            UnitModel miUnitModel = new UnitModel();
            miUnitModel.UnitAbbr = "mi";
            miUnitModel.UnitDescription = DistanceUnitMi.Text;

            distanceUnits.Add(kmUnitModel);
            distanceUnits.Add(miUnitModel);

            #endregion

            DistanceUnit.ItemsSource = distanceUnits;

            LoadDBDistanceUnit();
        }

        private void LoadDBDistanceUnit()
        {
            foreach (var item in distanceUnits)
            {
                if (item.UnitAbbr.Equals(App.distanceUnit))
                {
                    DistanceUnit.SelectedItem = item;
                    break;
                }
            }
        }

        private void MoneyUnit_Loaded(object sender, RoutedEventArgs e)
        {
            #region Initialize Money Unit

            UnitModel euroUnitModel = new UnitModel();
            euroUnitModel.UnitAbbr = "€";
            euroUnitModel.UnitDescription = MoneyUnitEuro.Text;

            UnitModel poundUnitModel = new UnitModel();
            poundUnitModel.UnitAbbr = "£";
            poundUnitModel.UnitDescription = MoneyUnitPound.Text;

            UnitModel dollarUnitModel = new UnitModel();
            dollarUnitModel.UnitAbbr = "$";
            dollarUnitModel.UnitDescription = MoneyUnitDollar.Text;


            moneyUnits.Add(euroUnitModel);
            moneyUnits.Add(poundUnitModel);
            moneyUnits.Add(dollarUnitModel);

            #endregion

            MoneyUnit.ItemsSource = moneyUnits;

            LoadDBMoneyUnit();
        }

        private void LoadDBMoneyUnit()
        {
            foreach (var item in moneyUnits)
            {
                if (item.UnitAbbr.Equals(App.moneyUnit))
                {
                    MoneyUnit.SelectedItem = item;
                    break;
                }
            }
        }

        private void VolumeUnit_Loaded(object sender, RoutedEventArgs e)
        {
            #region Initialize Volume Unit

            UnitModel litreUnitModel = new UnitModel();
            litreUnitModel.UnitAbbr = "ltr";
            litreUnitModel.UnitDescription = VolumeUnitLtr.Text;

            UnitModel gallonUnitModel = new UnitModel();
            gallonUnitModel.UnitAbbr = "gal";
            gallonUnitModel.UnitDescription = volumeUnitGal.Text;

            volumeUnits.Add(litreUnitModel);
            volumeUnits.Add(gallonUnitModel);

            #endregion

            VolumeUnit.ItemsSource = volumeUnits;

            LoadDBVolumeUnit();
        }

        private void LoadDBVolumeUnit()
        {
            foreach (var item in volumeUnits)
            {
                if (item.UnitAbbr.Equals(App.volumeUnit))
                {
                    VolumeUnit.SelectedItem = item;
                    break;
                }
            }
        }


        private void doneClick_Click(object sender, EventArgs e)
        {
            // salvo tutte le modifiche avvenute nelle unità di misura
            using (GestioneDBContext context = new GestioneDBContext(GestioneDBContext.ConnectionString))
            {
                UnitModel moneyU = MoneyUnit.SelectedItem as UnitModel;
                context.UnitMeasures.First().UnitMoney = moneyU.UnitAbbr;                
                App.moneyUnit = moneyU.UnitAbbr;

                UnitModel distanceU = DistanceUnit.SelectedItem as UnitModel;
                context.UnitMeasures.First().UnitDistance = distanceU.UnitAbbr;
                App.distanceUnit = distanceU.UnitAbbr;

                UnitModel volumeU = VolumeUnit.SelectedItem as UnitModel;
                context.UnitMeasures.First().UnitVolume = volumeU.UnitAbbr;
                App.volumeUnit = volumeU.UnitAbbr;

                // save all context
                context.SubmitChanges();
            }

            NavigationService.GoBack();
        }

        private void cancelClick_Click(object sender, EventArgs e)
        {
            NavigationService.GoBack();
        }

    }
}